|
|
|
|
|
|
|
Instead of creating a label to jump to when you want to handle errors, you can handle errors right after the line of code where the error has occurred. The statement On Error Resume Next tells Visual Basic that if an error occurs, the program should simply skip to the next line of code and bypass the line where the error has occurred. This type of error handling is not the most effective, and it can lead to code thats more difficult to read. However, in some instances, it will come in really handy. |
|
|
|
|
|
|
|
|
Examples Filename: KillFile.frm |
|
|
|
|
|
|
|
|
In the form shown in Figure 17.4, you input a filename to delete. If the file exists, its deleted using the Visual Basic statement Kill. If the file does not exist, a trappable error is generated. |
|
|
|
|
|
|
|
|
Figure 17.4
Some Resume Next examples. |
|
|
|
|
|
|
|
|
The following code shows how you can handle trappable errors: |
|
|
|
 |
|
|
|
|
Private Sub cmdResumeNext_Click()
' Delete a file, if it exists. On Error Resume Next
Stop
Kill txtFileKill
If Err.Number Then
Debug.Print "File Did Not Exist"
End If
Err.Clear
End Sub |
|
|
|
|
|
|
|
|
First, you set up your own error trap by using the On Error Resume Next statement. You then attempt the Kill statement on the filename in the text box. After the Kill statement, you can simply check the Err.Number property to see if its a value other than zero. If it is, you know that the file was not valid and nothing was deleted. This really isnt a problem because if youre trying to delete a file and that file has already been deleted, you dont really need to perform any processing at all. In fact, in a real application, I wouldnt even check the Err.Number in this case. |
|
|
|
|
|